package com.portal.admin.utils.weixni;

import com.portal.admin.base.constant.WxAppConst;
import com.portal.admin.base.enums.SystemType;
import com.portal.admin.utils.http.HttpClientUtils;
import com.portal.admin.utils.session.SessionUtils;
import net.sf.json.JSONObject;

import static com.portal.admin.config.service.SysConfigService.getSysConfig;

/**
 * 小程序工具类
 */
public class ClientApiUtils {

    public static JSONObject getJscode2session(String code){
        final String url = "https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=GRANT_TYPE";
        String reqUrl = url.replace("APPID", getSysConfig(WxAppConst.APPLET_APPID)).replace("SECRET", getSysConfig(WxAppConst.APPLET_APPSECRET)).replace("JSCODE", code).replace("GRANT_TYPE", "authorization_code");
        JSONObject result = HttpClientUtils.httpGet(reqUrl);
        SessionUtils.setSystemType(SystemType.WEIXIN.getValue());
        return result;
    }
    public static JSONObject getAccessToken(){
        final String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
        String reqUrl = url.replace("APPID", getSysConfig(WxAppConst.APPLET_APPID)).replace("APPSECRET", getSysConfig(WxAppConst.APPLET_APPSECRET));
        JSONObject result = HttpClientUtils.httpGet(reqUrl);
        return result;
    }
    public static JSONObject subscribeSend(String accessToken,JSONObject param){
        final String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=ACCESS_TOKEN";
        String reqUrl = url.replace("ACCESS_TOKEN", accessToken);
        JSONObject result = HttpClientUtils.httpPost(reqUrl,param);
        return result;
    }



}
